package com.java.mybatis.controller;

import com.java.mybatis.entity.Ordergoods;
import com.java.mybatis.service.OrdergoodsService;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Ordergoods)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/ordergoods/")
public class OrdergoodsController {
    /**
     * 服务对象
     */
    @Resource
    private OrdergoodsService ordergoodsService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Ordergoods selectOne(Integer id) {
        return this.ordergoodsService.queryById(id);
    }

    /**
     * 查询所以的数据
     * @param session
     * @param map
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("selectOrdergoods")
    public String selectOrdergoods(HttpSession session, Map map , Model model, Integer page) {
        Integer integer= null;//返回受影响的行数
        try {
            integer = ordergoodsService.ordergoodsCount();

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("--------------------" + page);
        if (page==null||page<=0) {
            page=1;
        }
        //查询所有的数据以及分页操作（分页:page）
        List<Ordergoods> ordergoods = ordergoodsService.GetAll((page-1)*6,6);
        //for循环输出数据
        for (Ordergoods ordergood : ordergoods) {
            System.out.println(ordergood);
        }
        //总共有多少页
        int goodPageMax = integer % 6 == 0 ? integer / 6 : integer / 6 + 1;
        model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
        //存入总共有多少页
        model.addAttribute("goodPageMax",goodPageMax);
        //分页当前页
        session.setAttribute("thisPage",page);
        //缓存数据（selectOrdergoods）
        model.addAttribute("selectOrdergoods",ordergoods);
        //缓存总行数（count）
        model.addAttribute("count",integer);
        return "System/comment-list";
    }

    /**
     * 模糊查询
     * @param model
     * @param str
     * @return
     * @throws Exception
     */
    @RequestMapping("selectOrdergoodslike")
    public String selectOrdergoodslike(Model model, String str)throws  Exception{
        if (str!=""&&str!=null) {
            List<Ordergoods> ordergoods = ordergoodsService.selectOrdergoodslike("%"+str+"%");
            model.addAttribute("selectOrdergoods",ordergoods);

            //模糊查询的总条数
            Integer i = ordergoodsService.selectOrdergoodslikeCount("%" + str + "%");
            System.out.println(i);
            model.addAttribute("pageText","当前第"+1+"页,共"+1+"页");

            model.addAttribute("count",i);
            return "System/comment-list";
        }else {
            return "redirect:/ordergoods/selectOrdergoods?page=1";
        }

    }

    /**
     * 修改商品单商品状态
     * @param ordergoodsId
     */
    @RequestMapping("updateOrdergoodsState")
    public void updateOrdergoodsCode(Integer ordergoodsId){
        int i = ordergoodsService.updateOrdergoodsState(ordergoodsId);

    }

    /**
     * 批量修改商品单商品状态
     * @param str
     * @return  updateSupplierCodeAll
     */
    @RequestMapping("updateOrdergoodsStateAll")
    public String updateOrdergoodsStateAll(String str){
        System.out.println(str);
        //str.split 用集合获取 再用 ， 分割获取
        String[] split = str.split(",");

        for (String s : split) {

            ordergoodsService.updateOrdergoodsState(Integer.parseInt(s.trim()));

        }
        return "redirect:/ordergoods/selectOrdergoods";
    }



    /**
     *报表查询用户下单地址的方法
     * @param model 用户下单地址
     * @return 对应的页面
     */
    @RequestMapping("queryLocation")
    public String queryLocation(Model model, HttpServletRequest request, HttpServletResponse response){
        HttpSession session=request.getSession();
        //调用查询用户下单地址的方法
        List<Ordergoods> ordergoods2 = ordergoodsService.queryLocation1();
        //调用查询商品名称的方法
        List<Ordergoods> ordergoods1 = ordergoodsService.selectGoodsName();
        //打印测试
        System.out.println(ordergoods2);
        //将查询到的数据存入model中
        model.addAttribute("ordergoods1",ordergoods1);
        //打印测试
        System.out.println(ordergoods1);
        //将获取到的数据存入session中
        session.setAttribute("ordergoods2",ordergoods2);
        //返回对应的页面
        return "System/echart-3";
    }

    /**
     * 查询商品名称的方法
     * @param model
     * @return
     */
    @RequestMapping("selectGoodsName")
    public String selectGoodsName(Model model){
        //返回对应的页面
        return "System/echart-3";
    }
}